package com.jinlejia.erp.ui.house;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.MyViewPagerAdapter;
import com.jinlejia.erp.databinding.ActivityInterestHouseBinding;
import com.jinlejia.erp.ui.customer.AddHouseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 兴趣房源
 */
public class InterestHouseActivity extends AppCompatActivity {

    // TabLayout中的tab标题  ,
    private String[] mTitles = {"兴趣房源", "兴趣小区房源"};

    private ActivityInterestHouseBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_interest_house);

        binding.btAdd.setOnClickListener(this::onClick);
        binding.btBack.setOnClickListener(this::onClick);

        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(0, InterestFragment.newInstance("1", getIntent().getStringExtra("type"),getIntent().getStringExtra("csCode")));
        mFragments.add(1, InterestFragment.newInstance("2", getIntent().getStringExtra("type"),getIntent().getStringExtra("csCode")));
        // 初始化ViewPager的适配器，并设置给它
        MyViewPagerAdapter mViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mTitles, mFragments);
        binding.viewpager.setAdapter(mViewPagerAdapter);
        binding.viewpager.setOffscreenPageLimit(2);

        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        binding.tablayout.setTabsFromPagerAdapter(mViewPagerAdapter);
        binding.tablayout.post(new Runnable() {
            @Override
            public void run() {
                // 将TabLayout和ViewPager进行关联，让两者联动起来
                binding.tablayout.setupWithViewPager(binding.viewpager);
            }
        });

    }


    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_add:
                shouPopupWindow();
                break;
            default:
                break;
        }
    }

    private void shouPopupWindow() {
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.pop_window, null);

        final PopupWindow popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        contentView.findViewById(R.id.house).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //添加兴趣房源
                popupWindow.dismiss();
                Intent intent = new Intent(InterestHouseActivity.this,AddHouseActivity.class);
                intent.putExtra("type",getIntent().getStringExtra("type"));
                intent.putExtra("addEnjoy",true);
                intent.putExtra("csCode",getIntent().getStringExtra("csCode"));
                startActivity( intent );
            }
        });
        contentView.findViewById(R.id.district).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //兴趣小区
                popupWindow.dismiss();
                Intent intent = new Intent(InterestHouseActivity.this,InterestDistrictActivity.class);
                intent.putExtra("csCode",getIntent().getStringExtra("csCode"));
                intent.putExtra("type",getIntent().getStringExtra("type"));
                startActivity(intent);
            }
        });

        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        // 设置好参数之后再show
        popupWindow.showAsDropDown(binding.btAdd);


    }
}
